<?php

namespace App\Console\Commands;

use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class CommandRepairColorData extends Command
{
    protected $signature = 'CommandRepairColorData';
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $connect = 'api';
    protected $gift_stream = 'gift_stream';//礼物流水
    protected $giftLib = 'gift_library';
    protected $colorGiftStream = 'color_gift_rate_award_stream';
    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '修复彩蛋数据';


    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    public function handle(){
        if(env('APP_ENV') != 'production'){
            $popArr = [
                "336" =>[347,355,343,345,340,342,348,353],
                "337" => [341,349,344,359,369,350,351,346],
                "338" => [370,356,366,354,367,368,362,361],
            ];
        }else{
            $popArr = [
                "336" =>[347,355,343,345,340,342,348,353],
                "337" => [341,349,344,359,369,350,351,346],
                "338" => [370,356,366,354,367,368,362,361],
            ];
        }
        do{
            try{
                //查询原有color_gift_rate_award_stream表数据
                $data = DB::connection($this->connect)->table($this->colorGiftStream)->where("color_gift_price",0)->get();
                $data = objToArray($data);
                if(empty($data)){
                    break;
                }
                foreach($data as $k=>$v){
                    if(in_array($v['award_gift_id'],$popArr[$v['color_id']])){
                        //彩蛋信息
                        $giftInfo = DB::connection($this->connect)->table($this->giftLib)->where('gift_id',$v['color_id'])->first();
                        $giftInfo = objToArray($giftInfo);
                        //更新数据
                        $update = [
                            'color_gift_price' => $giftInfo['price'],
                            'pool_rate_num' => 0,
                        ];
                        $res = DB::connection($this->connect)->table($this->colorGiftStream)->where('id',$v['id'])->update($update);
                        if($res){
                            echo '更新成功-|'.$v['id']." | \r\n";
                        }
                    }
                }
            }catch(\Exception $e){
                write_log('error_repair_color','error',['msg'=>$e->getMessage()]);
            }
        }while(false);
    
    }
}